package com.uduemc.biso.master.backend.request;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.validation.constraints.Future;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;

import com.uduemc.biso.core.extities.center.CustomerRole;
import com.uduemc.biso.core.extities.center.CustomerUser;
import com.uduemc.biso.core.extities.center.Host;
import com.uduemc.biso.core.extities.center.Site;
import com.uduemc.biso.core.extities.center.SysRole;
import com.uduemc.biso.master.backend.validator.CustomerUserUsernameCreate;
import com.uduemc.biso.master.backend.validator.HostServerIdExist;
import com.uduemc.biso.master.backend.validator.HostTypeIdExist;
import com.uduemc.biso.master.backend.validator.SiteLanguageIdsExist;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Accessors(chain = true)
public class CustomermanageAppend {

	@NotBlank(message = "登陆用户名不能为空")
	@Pattern(regexp = "\\w+", message = "登陆用户名只能是字母、数字、下划线组成")
	@Length(min = 4, max = 64, message = "登陆用户名的长度只能为4～64个字符")
	@CustomerUserUsernameCreate
	public String username;

	@NotBlank(message = "登陆密码不能为空！")
	@Length(min = 4, max = 32, message = "登陆密码的长度只能为4～32个字符")
	public String passwordHash;

	@NotBlank(message = "用户昵称不能为空")
	@Length(max = 32, message = "用户昵称不能超过32个字符")
	public String name;

	@Length(max = 32, message = "联系电话不能超过32个字符")
	public String phone;

	@Length(max = 32, message = "Email 不能超过32个字符")
	public String email;

	@HostTypeIdExist
	public long hostTypeId;

	@NotBlank(message = "站点名称不能为空")
	public String hostName;

	@HostServerIdExist
	public long hostServerId;

	@Range(min = 0, max = 1, message = "是绑定域名有、无效只能为 1、0 两个值")
	public short hostReview;

	@Range(min = 0, max = 1, message = "是试用、正式只能为 0、1 两个值")
	public short hostTrial;

	@NotNull(message = "到期时间不能为空")
	@Future(message = "到期时间要大于今天")
	public Date hostEndAt;

	@NotEmpty(message = "至少选择一种站点语言")
	@SiteLanguageIdsExist
	public List<Long> languageIds;

	public CustomerUser makeCustomerUser() {
		CustomerUser customerUser = new CustomerUser();
		customerUser.setName(this.getName()).setUsername(this.getUsername()).setPhone(this.getPhone())
				.setEmail(this.getEmail()).setPasswordHash(this.getPasswordHash());
		return customerUser;
	}

	public Host makeHost() {
		Host host = new Host();
		host.setTypeId(hostTypeId).setName(this.getHostName()).setServerId(this.getHostServerId())
				.setReview(this.getHostReview()).setTrial(this.getHostTrial()).setEndAt(this.getHostEndAt());

		return host;
	}

	public List<Site> makeSites() {
		List<Site> sites = new ArrayList<>();
		languageIds.sort(new Comparator<Long>() {
			@Override
			public int compare(Long o1, Long o2) {
				return (int) (o1.longValue() - o2.longValue());
			}
		});
		for (Long id : languageIds) {
			Site site = new Site();
			Integer languageId = Integer.valueOf(String.valueOf(id));
			site.setLanguageId(languageId).setStatus((short) 1);
			sites.add(site);
		}
		return sites;
	}

	public List<CustomerRole> makeCustomerRoles(List<SysRole> listSysRole) {
		List<CustomerRole> result = new ArrayList<>();
		for (SysRole role : listSysRole) {
			CustomerRole customerRole = new CustomerRole();
			customerRole.setRoleId(role.getId());
			result.add(customerRole);
		}
		return result;
	}
}
